<template>
  <div class="ant-upload-preview">
    <div style="width: 100%">
      <a-upload
        v-model:file-list="fileList"
        @change="handleChange"
        accept="image/jpg,image/jpeg,image/gif,image/png"
        list-type="picture-card"
        @remove="removeImage"
      >
        <div v-if="imageList.length !== imageNumber">
          <PlusOutlined />
          <div style="padding-top: 4px">上传</div>
        </div>
      </a-upload>
    </div>
    <!-- modal -->
    <cropper-modal
      ref="CropperModal"
      @cropper-no="handleCropperClose"
      @imgDataResultBlob="imgDataResultBlob"
    ></cropper-modal>
  </div>
</template>
<script>
import CropperModal from "./CropperModal.vue";
import { PlusOutlined } from "@ant-design/icons-vue";
export default {
  components: {
    CropperModal,
    PlusOutlined,
  },
  props: {
    //图片裁切配置
    options: {
      type: Object,
      default: function () {
        return {
          autoCrop: true, //是否默认生成截图框
          autoCropWidth: 180, //默认生成截图框宽度
          autoCropHeight: 180, //默认生成截图框高度
          fixedBox: false, //是否固定截图框大小 不允许改变
          previewsCircle: true, //预览图是否是原圆形
          title: "修改头像",
        };
      },
    },
    imageNumber: {
      type: [Number, String],
      default: 0,
    },
    imageFileList: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      loading: false,
      isStopRun: false,
      imageUrl: "",
      fileList: [],
      index: 0,
      imageList: [],
    };
  },
  methods: {
    // 详情图片
    detailsImage(link) {
      if (link) {
        const { imageFileList } = this;
        if (imageFileList) {
          // this.fileList.push(url);
          this.fileList = link.map((item, index) => {
            return {
              uid: index,
              url: item,
            };
          });
        }

        this.imageList = link.map((item, index) => {
          return {
            uid: index,
            url: item,
          };
        });
      }
    },
    deleteImage(iamges) {
      this.imageList = iamges.map((item, index) => {
        return {
          uid: index,
          url: item,
        };
      });
    },
    // 关闭弹框清除图片
    clearImage() {
      this.fileList = [];
      this.imageList = [];
    },
    //从本地选择文件
    handleChange(info) {
      if (this.isStopRun) {
        return;
      }
      this.loading = true;
      const { options } = this;
      this.fileList = info.fileList.filter((item) => item.url);
      this.getBase64(info.file.originFileObj, (imageUrl) => {
        const target = Object.assign({}, options, {
          img: imageUrl,
        });
        this.$refs.CropperModal.edit(target);
      });
    },
    removeImage(file) {
      this.fileList = this.fileList.filter((item) => item.url !== file.url);
      this.imageList = this.fileList.filter((item) => item.url !== file.url);
      this.$emit("remove", this.fileList);
      // const { imageNumber } = this;
      // if (imageNumber == 1) {
      //   this.fileList = [];
      //   this.imageList = [];
      // }
    },
    // 取消上传
    handleCropperClose() {
      this.loading = false;
      this.$emit("crop-upload-close");
    },
    getBase64(img, callback) {
      if (img) {
        const reader = new FileReader();
        reader.addEventListener("load", () => callback(reader.result));
        reader.readAsDataURL(img);
      }
    },
    imgDataResultBlob(data) {
      // const myBlob = new window.Blob([data], { type: "image/jpeg" });
      this.index = this.index + 1;

      // // 转成url 可用来回显
      let url = {
        uid: this.index,
        url: data,
      };
      const { imageFileList } = this;
      if (imageFileList) {
        this.fileList.push(url);
      }
      this.imageList.push(url);
      this.$emit("crop-upload-success", this.imageList);
    },
  },
};
</script>

<style lang="less" scoped>
.avatar-upload-wrapper {
  height: 180px;
  width: 100%;
}

.ant-upload-preview {
  background-color: #fff;
  .avatar-uploader > .ant-upload {
    width: 128px;
    height: 128px;
  }
  .ant-upload-select-picture-card i {
    font-size: 32px;
    color: #999;
  }
  .upload_img {
    width: 100%;
  }
  .ant-upload-select-picture-card .ant-upload-text {
    margin-top: 8px;
    color: #666;
  }
}
</style>
